﻿using CommunityToolkit.Mvvm.Input;
using Koala.Pro.Common;
using Koala.Pro.Core.Models;
using Koala.Pro.Services;
using Koala.Pro.ViewModels;
using System.Data;
using System.Windows.Controls;
using System.Windows.Input;
using Wpf.Ui.Controls;

namespace Koala.Pro.Views.Pages
{
    public partial class SystemTypeListPage : Page, INavigableView<SystemTypeListViewModel>
    {
        public SystemTypeListViewModel ViewModel { get; }

        public SystemTypeListPage(SystemTypeListViewModel viewModel)
        {
            ViewModel = viewModel;
            base.DataContext = this;
            InitializeComponent();
        }

        [RelayCommand]
        private async void Add()
        {
            await Edit(null);
        }

        [RelayCommand]
        private async Task Edit(SystemTypeInfo info)
        {
            SystemTypeEditPage page = App.GetService<SystemTypeEditPage>();
            if (info != null)
            {
                SystemTypeEditViewModel viewModel = page.ViewModel;
                viewModel.Item = await BLLFactory<ISystemTypeService>.Instance.GetAsync(info.Id);
                page.ViewModel.IsEdit = true;
            }
            else
            {
                page.ViewModel.Item = new SystemTypeInfo();
                page.ViewModel.IsEdit = false;
            }
            page.OnDataSaved += delegate
            {
                Search();
            };
            page.ShowDialog();
        }

        [RelayCommand]
        private async Task Delete(SystemTypeInfo info)
        {
            await ViewModel.DeleteCommand.ExecuteAsync(info);
        }

        [RelayCommand]
        private void Search()
        {
            ViewModel.SearchCommand.ExecuteAsync(null);
        }

        private async void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if ((sender as System.Windows.Controls.DataGrid)?.SelectedItem is SystemTypeInfo info)
            {
                await Edit(info);
            }
        }

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!(sender is System.Windows.Controls.DataGrid { SelectedItems: var selectedItems }))
            {
                return;
            }
            if (selectedItems != null)
            {
                List<SystemTypeInfo> list = new List<SystemTypeInfo>();
                foreach (SystemTypeInfo item in selectedItems)
                {
                    list.Add(item);
                }
                ViewModel.SelectedItems = list;
            }
            else
            {
                ViewModel.SelectedItems = null;
            }
        }

        [RelayCommand]
        private void ImportExcel()
        {
            var page = App.GetService<ImportExcelPage>();
            page.ViewModel.Items?.Clear();
            page.ViewModel.TemplateFile = "系统类型信息-模板.xls";
            page.OnDataSave -= ExcelData_OnDataSave;
            page.OnDataSave += ExcelData_OnDataSave;
            ViewModel.Navigate(typeof(ImportExcelPage));
        }

        private string GetRowData(DataRow row, string columnName)
        {
            string result = "";
            if (row.Table.Columns.Contains(columnName))
            {
                result = row[columnName].ToString();
            }
            return result;
        }

        private async Task<bool> ExcelData_OnDataSave(DataRow dr)
        {
            Convert.ToDateTime("1900-01-01");
            SystemTypeInfo systemTypeInfo = new SystemTypeInfo();
            systemTypeInfo.Id = GetRowData(dr, "系统标识");
            systemTypeInfo.Name = GetRowData(dr, "系统名称");
            systemTypeInfo.CustomID = GetRowData(dr, "客户编码");
            systemTypeInfo.Authorize = GetRowData(dr, "授权编码");
            systemTypeInfo.Note = GetRowData(dr, "备注");
            return await BLLFactory<ISystemTypeService>.Instance.InsertAsync(systemTypeInfo);
        }
    }
}
